package cn.com.cfae.iras.doc.analyze.components.rationality;

import cn.com.cfae.iras.doc.analyze.components.BaseComponent;
import cn.com.cfae.iras.doc.analyze.components.beans.ComponentResult;
import cn.com.cfae.iras.doc.analyze.handUtil.HandingData;
import cn.com.cfae.iras.doc.analyze.parse.kv.KVText;
import cn.com.cfae.iras.doc.analyze.parse.model.ChapterModel;
import cn.com.cfae.iras.doc.analyze.parse.model.DocumentModel;
import cn.com.cfae.iras.doc.analyze.parse.model.ExtractBizModel;
import cn.com.cfae.iras.doc.analyze.parse.model.ExtractItem;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

;

/**
 * 内容合理性
 * M-3-1
 * 发行价格：检查是否为“100元”
 * 发行阶段
 */
@Component("R1017")
@Scope("prototype")
public class R1017 extends BaseComponent {
    private static Logger log = LoggerFactory.getLogger(R1017.class);
    @Autowired
    private HandingData handingData;
    @Override
    public List<ComponentResult> handlerM() {
        log.info("获取R1017组件M");
        List<ComponentResult> componentResults = checkRulesR1017(getCatalogId(),getCatalogId(),getChapterId(),getRuleTitle());
        return componentResults;
    }

    @Override
    public List<ComponentResult> handlerBM() {
        log.info("获取R1017组件BM");
        List<ComponentResult> componentResults = checkRulesR1017(getCatalogId(),getCatalogId(),getChapterId(),getRuleTitle());
        return componentResults;
    }

    @Override
    public List<ComponentResult> handlerDM() {
        log.info("获取R1017组件DM");
        List<ComponentResult> componentResults = checkRulesR1017(getCatalogId(),getCatalogId(),getChapterId(),getRuleTitle());
        return componentResults;
    }

    @Override
    public List<ComponentResult> handlerBDM() {
        log.info("获取R1017组件BDM");
        List<ComponentResult> componentResults = checkRulesR1017(getCatalogId(),getCatalogId(),getChapterId(),getRuleTitle());
        return componentResults;
    }

    private List<ComponentResult> checkRulesR1017(String model,String catalogId,String chapterId,String ruleName){
        DocumentModel extractData = super.getExtractData();
        List<ComponentResult> componentResults = new ArrayList<>();
        ComponentResult componentResult = null;
        ExtractItem extractItem = null;
        ComponentResult fxjgComponentResult = null;
        int fxjgPageNum = 0;
        List<ComponentResult.TextPosition> fxjgContentAndPositionList = null;
        boolean fxjgFlag = false;
        int startPage = 0;
        try {
            if (extractData!=null){
                ChapterModel chapterModel = extractData.getChapterModel(model);
                startPage = chapterModel!=null?chapterModel.getStartPage():0;
                //发行价格
                extractItem = getExtractItem(extractData,model,"FXTK","FXJG");
                if (extractItem!= null){
                    KVText value = extractItem.getValue();
                    KVText key = extractItem.getKey();
                    String text = value!=null?value.getText():null;
                    fxjgPageNum = value!=null?value.getPageNumber():startPage;
                    fxjgPageNum = fxjgPageNum!=0?fxjgPageNum:startPage;
                    fxjgContentAndPositionList = key!=null?getContentAndPositionList(key):null;
                    String[] synonyms = new String[]{"人民币壹佰元","100元","RMB100.00元","人民币100元","￥100","壹佰元"};
                    if (StringUtils.isNotEmpty(text)){
                        text = text.replaceAll("\\s","");
                        for (String synonym : synonyms){
                            if (text.contains(synonym)){
                                fxjgFlag = true;
                                break;
                            }
                        }
                        if (!fxjgFlag){
                            if (text.contains("面值")){
                                extractItem = getExtractItem(extractData,model,"FXTK","PJMZ");
                                if (extractItem!=null){
                                    KVText mzValue = extractItem.getValue();
                                    String mzText = mzValue!=null?mzValue.getText():null;
                                    if (StringUtils.isNotEmpty(mzText)){
                                        mzText = mzText.replaceAll("\\s","");
                                        for (String synonym : synonyms){
                                            if (mzText.contains(synonym)){
                                                fxjgFlag = true;
                                                break;
                                            }
                                        }
                                    }
                                }
                            }
                        }

                        if (fxjgFlag){
                            fxjgComponentResult =  handingData.getComponentResult(catalogId,chapterId,ruleName,"发行价格检查-是否为100元","合理",false,getRegulationId(), fxjgPageNum,fxjgContentAndPositionList,"01");
                            componentResults.add(fxjgComponentResult);

                        }else {
                            fxjgComponentResult =  handingData.getComponentResult(catalogId,chapterId,ruleName,"发行价格检查-是否为100元","发行价格,请人工核对",true,getRegulationId(), fxjgPageNum,fxjgContentAndPositionList,"02");
                            componentResults.add(fxjgComponentResult);
                        }

                    }else {
                        fxjgComponentResult =  handingData.getComponentResult(catalogId,chapterId,ruleName,"发行价格检查-是否为100元","未识别,建议人工核对",true,getRegulationId(), startPage,fxjgContentAndPositionList,"03");
                        componentResults.add(fxjgComponentResult);
                    }

                }else {
                    fxjgComponentResult =  handingData.getComponentResult(catalogId,chapterId,ruleName,"发行价格检查-是否为100元","未识别,建议人工核对",true,getRegulationId(), startPage,fxjgContentAndPositionList,"03");
                    componentResults.add(fxjgComponentResult);
                }
            }

        }catch (Exception e){
            componentResults.clear();
            fxjgComponentResult =  handingData.getComponentResult(catalogId,chapterId,ruleName,"发行价格检查-是否为100元","未识别,建议人工核对",true,getRegulationId(), startPage,fxjgContentAndPositionList,"03");
            componentResults.add(fxjgComponentResult);
            log.error(e.getMessage(),e);
        }
        return componentResults;
    }

    private ExtractItem getExtractItem(DocumentModel extractData, String chapter, String model, String key){
        ExtractItem extractItem  = null;
        if (extractData!=null){
            ChapterModel chapterModel = extractData.getChapterModel(chapter);
            if (chapterModel != null){
                ExtractBizModel extractMode = chapterModel.getModel(model);
                if (extractMode!=null){
                    extractItem = extractMode.getExtractItem(key);
                }
            }
        }
        return extractItem;
    }
}
